package GUI;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.util.ArrayList;

@SuppressWarnings("serial")
/**
 * Places an Order of a specific copy for the user
 * @author Benny
 *
 */
public class OrderBookGUI extends JPanel {
	private JTextField textCopyID;
	public MainFrame mf;
	/**
	 * initialize all the text fields of the GUI
	 */
	public void init(){
		textCopyID.setText("");
	}
	
	/**
	 * check for the validity of the specified copyID
	 * @return copyID validity status
	 */
	public int checkCopyID(){
		try  
		{  
			@SuppressWarnings("unused")
			int i = Integer.parseInt(textCopyID.getText());  
		}  
		catch(NumberFormatException nfe)  
		{  
			return 0;  
		}  
		return 1;  
	}
	/**
	 * Create the panel.
	 *  @param M Instance of the main Frame of the program 
	 */
	public OrderBookGUI(MainFrame M) {
		setLayout(null);
		mf=M;
		JLabel lblOrderBook = new JLabel("Order Book");
		lblOrderBook.setFont(new Font("Segoe UI", Font.BOLD, 16));
		lblOrderBook.setBounds(245, 30, 103, 14);
		add(lblOrderBook);
		
		JLabel lblcopyID = new JLabel("copyID:");
		lblcopyID.setBounds(162, 81, 103, 14);
		add(lblcopyID);
		
		textCopyID = new JTextField();
		textCopyID.setBounds(308, 78, 134, 20);
		add(textCopyID);
		textCopyID.setColumns(10);
		
		JButton btnOrder = new JButton("Order");
		btnOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(checkCopyID()==1)
				{
					Integer id=mf.logged_in_user.id;
					ArrayList<Object> send = new ArrayList<>();
					int copyID=Integer.decode(textCopyID.getText());
					send.add("1");
					send.add("Order");
					send.add(id);
					send.add(copyID);
					mf.client.accept(send);
				}
				else JOptionPane.showMessageDialog(null, "copyID must be a number", "Program", JOptionPane.ERROR_MESSAGE);
			}
		});
		btnOrder.setBounds(164, 126, 89, 23);
		add(btnOrder);
		
		JButton btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				init();
			}
		});
		btnClear.setBounds(329, 126, 89, 23);
		add(btnClear);
		
		ImageIcon img = new ImageIcon(getClass().getResource(
                "/util/grad2.jpg"));
		JLabel picLabel = new JLabel(img);
		picLabel.setLocation(0, 0);
		picLabel.setSize(683, 575);
		add(picLabel);

	}

}
